package com.youme.section

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.hyphenate.easecallkit.EaseCallKit
import com.hyphenate.easecallkit.base.EaseCallFloatWindow
import com.hyphenate.easecallkit.base.EaseCallType
import com.hyphenate.easecallkit.ui.EaseMultipleVideoActivity
import com.hyphenate.easecallkit.ui.EaseVideoCallActivity
import com.hyphenate.easecallkit.utils.EaseCallState
import com.hyphenate.easeim.common.enums.Status
import com.hyphenate.easeim.common.interfaceOrImplement.OnResourceParseCallback
import com.hyphenate.easeim.common.net.Resource
import com.hyphenate.easeim.section.login.viewmodels.SplashViewModel
import com.hyphenate.util.EMLog
import com.youme.RouterManager

class SplashActivity : AppCompatActivity() {
    private lateinit var splashViewModel: SplashViewModel
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        splashViewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
        splashViewModel.loginData.observe(this,
            Observer { response: Resource<Boolean?> ->
                parseResource(response, object : OnResourceParseCallback<Boolean?>(true) {
                    override fun onSuccess(data: Boolean?) {
                        loginSuccess()
                    }

                    override fun onError(code: Int, message: String) {
                        super.onError(code, message)
                        EMLog.i("TAG", "error message = " + response.message)
                        RouterManager.smsLogin(this@SplashActivity)
                        finish()
                    }
                })
            })
    }

    private fun loginSuccess() {
        RouterManager.main(this@SplashActivity)
    }

    fun <T> parseResource(
        response: Resource<T?>,
        callback: OnResourceParseCallback<T?>
    ) {
        if (response == null) {
            return
        }
        if (response.status == Status.SUCCESS) {
            callback.hideLoading()
            callback.onSuccess(response.data)
        } else if (response.status == Status.ERROR) {
            callback.hideLoading()
            if (!callback.hideErrorMsg) {
                ToastUtils.show(response.message)
            }
            callback.onError(response.errorCode, response.message)
        } else if (response.status == Status.LOADING) {
            callback.onLoading(response.data)
        }
    }

    override fun onRestart() {
        super.onRestart()
        if (EaseCallKit.getInstance().callState != EaseCallState.CALL_IDLE &&
            !EaseCallFloatWindow.getInstance(applicationContext).isShowing) {
            if (EaseCallKit.getInstance().callType == EaseCallType.CONFERENCE_CALL) {
                val intent = Intent(applicationContext, EaseMultipleVideoActivity::class.java)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                applicationContext.startActivity(intent)
            } else {
                val intent = Intent(applicationContext, EaseVideoCallActivity::class.java).addFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK)
                applicationContext.startActivity(intent)
            }
        }
    }
}